/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aurelius.testngexecutionlogger;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

/**
 *
 * @author Lukasz
 */
public class EventListener implements ITestListener {

    @Override
    public void onTestStart(ITestResult result) {
    }

    @Override
    public void onTestSuccess(ITestResult result) {
    }

    @Override
    public void onTestFailure(ITestResult result) {
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        process(result);
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        process(result);
    }

    @Override
    public void onStart(ITestContext context) {
    }

    @Override
    public void onFinish(ITestContext context) {
    }

    private void process(ITestResult testResult) {
        StringBuilder stringBuilder = new StringBuilder(50);
        stringBuilder.append(" ");

        int status = testResult.getStatus();
        if (ITestResult.FAILURE == status) {
            stringBuilder.append("[");
            stringBuilder.append("FAIL");
            stringBuilder.append("]");
        } else if (ITestResult.SUCCESS == status) {
            stringBuilder.append("[");
            stringBuilder.append("PASS");
            stringBuilder.append("]");
        } else if (ITestResult.SKIP == status) {
            stringBuilder.append("[");
            stringBuilder.append("SKIP");
            stringBuilder.append("]");
        } else if (ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {
            stringBuilder.append("[");
            stringBuilder.append("SUCCESS_PERCENTAGE_FAILURE");
            stringBuilder.append("]");
        }

//        stringBuilder.append(" Class: ");
        stringBuilder.append(" - ");
        stringBuilder.append(testResult.getTestClass().getRealClass().getSimpleName());
//        stringBuilder.append(" method name: ");
        stringBuilder.append(" - ");
        stringBuilder.append(testResult.getMethod().getMethodName());
        stringBuilder.append(" - ");
        stringBuilder.append(testResult.getEndMillis() - testResult.getStartMillis());

        TestResultLogger.log(stringBuilder.toString());
    }
}
